//
//  LoginViewController.m
//  RentCar
//
//  Created by sunmingming on 14-4-1.
//  Copyright (c) 2014年 Ebola. All rights reserved.
//

#import "LoginViewController.h"
#import "Register1ViewController.h"
#import "ForgetPWViewController.h"

@interface LoginViewController ()

@property(nonatomic,weak) IBOutlet UIImageView *zyImage;
@property(nonatomic,weak) IBOutlet UILabel *zyLabel;

@property(nonatomic,weak) IBOutlet UITextField *nameField;
@property(nonatomic,weak) IBOutlet UITextField *passField;

@property(nonatomic,weak) IBOutlet UIButton *loginButton;
@property(nonatomic,weak) IBOutlet UIButton *forgetPassButton;

-(IBAction)loginPressed:(id)sender;
-(IBAction)forgetPassPressed:(id)sender;
-(IBAction)directPressed:(id)sender;

@end

@implementation LoginViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"登录领头洋";
        self.navigationItem.rightBarButtonItem =
        [[UIBarButtonItem alloc] initWithTitle:@"注册"
                                         style:UIBarButtonItemStyleDone
                                        target:self
                                        action:@selector(registerPressed:)];
        
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self showError:nil];
}

-(void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
    self.loginButton.enabled = YES;
    if(APPALL.myUserItem.username.length)
    {
        self.nameField.text = APPALL.myUserItem.username;
        self.passField.text = APPALL.myUserItem.password;
    }
    else
    {
        self.nameField.text = @"";
        self.passField.text = @"";
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//登录请求
-(IBAction)loginPressed:(id)sender{
    if(self.nameField.text.length <= 0)
    {
        [self showError:@"请填写手机号！"];
        return;
    }
    if(self.passField.text.length <= 0)
    {
        [self showError:@"请填写密码！"];
        return;
    }
    [SVProgressHUD showWithStatus:@"loading..."];
    [self resignAllTextField];
    [self showError:nil];
    NSDictionary *parameters = @{@"username": self.nameField.text,
                         @"password":self.passField.text};
    [SVProgressHUD showWithStatus:@"loading..."];
    [APPALL.netSession POST:[NSString stringWithFormat:@"%@%@",APIURL,APILogin]
                 parameters:parameters
                   progress:nil
                    success:^(NSURLSessionDataTask * task, id responseObject){
                        NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                        NSLog(@"JSON: %@", JSON);
                        if([JSON[@"login"] isEqualToString:@"success"]){
                            NSDictionary *infoJson = JSON[@"user_info"];
                            APPALL.myUserItem.username = self.nameField.text;
                            APPALL.myUserItem.password = self.passField.text;
                            APPALL.myUserItem.age = infoJson[@"age"];
                            APPALL.myUserItem.height = infoJson[@"height"];
                            APPALL.myUserItem.nickname = infoJson[@"nickname"];
                            APPALL.myUserItem.phone = infoJson[@"phone"];
                            APPALL.myUserItem.qq = infoJson[@"qq"];
                            APPALL.myUserItem.weibo = infoJson[@"weibo"];
                            APPALL.myUserItem.weight = infoJson[@"weight"];
                            APPALL.myUserItem.weixin = infoJson[@"weixin"];
                            [APPALL.myUserItem saveToDB];
                            [APPALL dismissLoginWindow];
                            [SVProgressHUD dismiss];
                            [self.navigationController popViewControllerAnimated:YES];
                        }else{
                            [SVProgressHUD showErrorWithStatus:@"登录失败"];
                        }
                    }
                    failure:^(NSURLSessionDataTask * task, NSError * error){
                        [SVProgressHUD dismiss];
                        //[SVProgressHUD showErrorWithStatus:[NSString stringWithFormat:@"请求失败:%@", error.description]];
                    }];
}


-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self resignAllTextField];
}

-(void)resignAllTextField
{
    [self.nameField resignFirstResponder];
    [self.passField resignFirstResponder];
}

-(void)registerPressed:(id)sender
{
    Register1ViewController *rvc = [Register1ViewController new];
    [self.navigationController pushViewController:rvc animated:YES];
}

-(IBAction)forgetPassPressed:(id)sender
{
    ForgetPWViewController *rvc = [[ForgetPWViewController alloc] init];
    [self.navigationController pushViewController:rvc animated:YES];
}

-(IBAction)directPressed:(id)sender
{
    [self resignAllTextField];
    [self showError:nil];
    [APPALL dismissLoginWindow];
}

-(void)showError:(NSString*)aStr
{
    if((!aStr) || aStr.length <= 0)
    {
        self.zyImage.hidden = YES;
        self.zyLabel.hidden = YES;
    }
    else
    {
        self.zyImage.hidden = NO;
        self.zyLabel.hidden = NO;
        self.zyLabel.text = aStr;
    }
}

-(void)dealloc
{
    self.nameField = nil;
    self.passField = nil;
    self.loginButton = nil;
    self.forgetPassButton = nil;
}
@end
